﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;
using System.Web.Script.Serialization;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_CustorList : CommonPageBase
    {

        /// <summary>
        /// 取得权限
        /// </summary>
        /// <returns></returns>
        public bool GetQX()
        {

            bool n = true;
            string UserKeys = UserPower.GetHaveKey();
            if (!UserKeys.Contains("View"))
            {
                PageContext.RegisterStartupScript("alert('您未有查看权限！');RemTab('dynamic_added_tab_tab_" + UserPower.GetKeyWord() + "');");
                n = false;
            }
            else
            {
                //if (CookInfo.EAdminTrueName().Trim() == "陈爱国")
                //{
                //    Button1.Hidden = false;
                //}
                if (!UserKeys.Contains("Add"))
                {
                    btnNew.Hidden = true;
                }

                if (!UserKeys.Contains("Del"))
                {
                    Button2.Hidden = true;
                }
                if (!UserKeys.Contains("Edit"))
                {
                    Button1.Hidden = true;
                }
            }
            return n;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // if (GetQX())
                // {
                //  Response.Write(Window1.GetShowReference("asdfasdf.aspx", "查看回访记录"));
                btnNew.OnClientClick = Window1.GetShowReference("Pro_CustorChange.aspx", "添加客户资料");
                BindGrid(GetWhere());
                //}
            }
        }


        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<VI_ProCusInfo>().Where(wc).Count();

            List<VI_ProCusInfo> items = TbsDbSession.Default.From<VI_ProCusInfo>().Where(wc).OrderBy(VI_ProCusInfo._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            //if (!string.IsNullOrEmpty(Request["did"]))
            //{
            //    wc = wc && Adm_BASICD._.BATYPE == int.Parse(Request["did"]);
            //}
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (VI_ProCusInfo._.CUTNUB.Contain(ttbxMyBox2.Text) || VI_ProCusInfo._.CUTNAM.Contain(ttbxMyBox2.Text));
            }
            return wc;
        }
        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }
        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string p = e.Values[10].ToString();
            string id = e.Values[11].ToString();
            if (p != "")
            {
                p = p.Replace("&quot;", "\"");
                p = p.Replace("\\\"", "\"");
                p = "[" + p.Substring(0, p.Length - 1) + "]";
                e.Values[0] = e.Values[10];
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<Sale_VPlant> sv = js.Deserialize<List<Sale_VPlant>>(p);
                sb.Append("<table id=\"mytable\" cellspacing=\"0\" summary=\"The technical specifications of the Apple PowerMac G5 series\">");
                sb.Append("<tr>");
                sb.Append("<th scope=\"col\" abbr=\"Configurations\" style=\"width:30%\">拜访日期</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 1.8\">客户联系人</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 2\">拜访人员</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 2.5\">拜访类型</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 2.5\" style=\"width:20%\">拜访目的</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 2.5\">拜访状态</th>");
                sb.Append("<th scope=\"col\" abbr=\"Dual 2.5\">查看</th>");
                sb.Append("</tr>");
                foreach (Sale_VPlant item in sv)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>" + item.STIME.Value.ToString("yyyy-MM-dd HH:mm") + " ~ " + item.ETIME.Value.ToString("yyyy-MM-dd HH:mm") + "</td>");
                    sb.Append("<td>" + item.CONTAT + "</td>");
                    sb.Append("<td>" + item.VIPERSON + "</td>");
                    sb.Append("<td>" + item.VITYPE + "</td>");
                    sb.Append("<td>" + item.OBJECTIVE + "</td>");
                    sb.Append("<td>" + item.STATUS + "</td>");
                    sb.Append("<td><a href=\"javascript:;\" onclick=\"X('Window2').x_show('/Sale/Sale_VPlantChange.aspx?s=1&id=" + item.ID + "','查看拜访记录');\">查看</a></td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");
                e.Values[0] = sb.ToString();
            }
            e.Values[1] = "<a href=\"javascript:;\" onclick=\"X('Window2').x_show('/Sale/Sale_VPlantChange.aspx?cid=" + id + "','添加拜访记录');\">" + e.Values[1] + "</a>";
            //string color = e.Values[7].ToString();
            //string bold = e.Values[8].ToString();
            //if (bold == "True")
            //    bold = "bold";
            //else
            //    bold = "";
            //string title = "<span style=\"color:{0};font-weight:{1}\">" + e.Values[0] + "</span>";
            //title = string.Format(title, color, bold);
            //e.Values[0] = title;
            //e.Values[7] = "<a href=\"javascript:;\" onclick=\"TabView('/AfterSale/ASale_RePairChange.aspx?id=" + e.Values[7] + "', 'tab_wxadd', '修改维修记录', '/icon/comment_edit.png');\"><img src=\"/icon/comment_edit.png\" /></a>";
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<VI_ProCusInfo>(VI_ProCusInfo._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
            //if (e.CommandName == "CheckBox1")
            //{
            //    CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn(e.ColumnIndex);
            //    bool checkState = checkField.GetCheckedState(e.RowIndex);
            //    Proj_Custor tea = new Proj_Custor();
            //    tea.ID = id;
            //    tea.Attach();
            //    tea.COMBIN = checkState;
            //    TbsDbSession.Default.Update<Proj_Custor>(tea);
            //    BindGrid(GetWhere());
            //}
        }

        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            PageContext.RegisterStartupScript(Window1.GetShowReference("Pro_CustorChange.aspx?id=" + id, "修改"));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FormInfo.GridEdit(Grid1, Window1, "Pro_CustorChange.aspx");
            BindGrid(GetWhere());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FormInfo.GridDelete<VI_ProCusInfo>(Grid1, Window1, new VI_ProCusInfo());
            BindGrid(GetWhere());
        }
    }
}